﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace com.iot.core.util
{
    public class StringUtil
    {
        public static string Join<T>(List<T> l, string joinchar = ",")
        {
            var s = "";
            for (var i = 0; i < l.Count; i++)
            {
                if (i != 0)
                {
                    s += joinchar;
                }
                s += l[i].ToString();
            }
            return s;
        }
        public static string DistinctJoin<T>(List<T> l)
        {
            var l1 = l.Distinct().ToList();
            var s = "";
            for (var i = 0; i < l1.Count; i++)
            {
                if (string.IsNullOrEmpty(l1[i].ToString()))
                {
                    continue;
                }
                s += l1[i].ToString() + ",";

            }
            return s.TrimEnd(new char[] { ',' });
        }

        public static List<long> SplitToLong(string s)
        {
            List<long> l = new List<long>();
            if (string.IsNullOrEmpty(s))
            {
                return l;
            }
            s.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries).ToList().ForEach(id =>
            {
                l.Add(long.Parse(id));
            });
            return l;
        }
        public static List<string> SplitToStr(string s)
        {
            List<string> l = new List<string>();
            s.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries).ToList().ForEach(id =>
            {
                l.Add(id.Trim() );
            });
            return l;
        }

    }
}
